﻿#include "mclientmanage.h"
#include "mclient.h"
#include <QDebug>

#include <QThread>

class MClientManagePrivate
{
public:
    MClientManagePrivate() {
        client = NULL;
    }
public:
    MClient *client;
    QThread *thread;
};

MClientManage::MClientManage(QObject *parent) 
    : QObject(parent)
{
    d = new MClientManagePrivate();
    buildConnect();
}

MClientManage::~MClientManage()
{
    d->thread->exit(0);
    d->thread->wait();
    delete d;
}

void MClientManage::sendDataFun(const QString &data)
{
    emit sendData(data);
}
 
void MClientManage::buildConnect()
{
    d->client = new MClient("127.0.0.1",6667,nullptr);
    
    d->thread = new QThread(this);
    d->thread->start();
    d->client->moveToThread(d->thread);
    
    //connect(d->client,SIGNAL(stateChanged(int)),this,SLOT(stateChanged(int)));
    connect(d->client,SIGNAL(newCommand(QVariant)),this,SIGNAL(newCommand(QVariant)));
    connect(d->client,SIGNAL(newRefresh(QVariant)),this,SIGNAL(newRefresh(QVariant)));
    connect(this,SIGNAL(sendData(QString)),d->client,SLOT(sendData(QString)));
    
    connect(d->thread,SIGNAL(started()),d->client,SLOT(startConnect()));
    connect(d->thread,SIGNAL(finished()),d->client,SLOT(deleteLater()));
    
    
}

void MClientManage::tFinished()
{
    qWarning()<<"-------tFinished------";
}
